<template>
	<view class="parent-view">
		<scroll-view class="address-sv">
			<view class="address-list">
				<view class="address-item" v-for="(item,index) in addressList" :key="index">
					<view class="name-view" @click="selectAddress"  :data-index="item">
						<text class="name-txt">{{item.name}}</text>
						<text class="phone-txt">{{item.phone}}</text>
					</view>
					<view @click="selectAddress" :data-index="item" class="address-txt-view"><image class="address-icon" 
					src="../../static/address-icon.png"></image>{{item.address+item.detail}}</view>
					<view class="options-view">
						<image class="select-icon" :src="item.isDefault==1?'../../static/icon-complete-info-select.png'
					:'../../static/icon-complete-info-unselect.png'" @click="setMallAddress" :data-index="item"></image>
						<text class="select-txt" @click="setMallAddress" :data-index="item">设为默认</text>
						<text class="bianji-btn" @click="bianjiAddress" :data-index="item">编辑</text>
						<text class="shanchu-btn" @click="delMallAddress" :data-index="item">删除</text>
					</view>
				</view>
			</view>
		</scroll-view>
		<view class="add-address-btn" @click="addAddress">新增地址</view>
	</view>
</template>

<script>
	import request from '../index/request.js';
	export default {
		data() {
			return {
				addressList:[],
			}
		},
		methods: {
			onLoad: function(options) {
				this.appUtil.saveAppInviteId(options)
			},
			onShow:function(){
				this.share.url='/pages/tabBar/home/index?appInviteId='+this.appUtil.getAppInviteId()
				this.share.shareType=1
				this.getUserAddressList()
			},
			getUserAddressList:function(){
				var that=this
				request.post("api/mall/GetUserAddressList", {
				}).then(data => {
					that.addressList=data
				})
			},
			delMallAddress:function(e){
				var data=e.currentTarget.dataset.index
				var that=this
				request.post("api/mall/DelUserAddress", {
					addressid:data.id
				}).then(data => {
					that.getUserAddressList()
				})
			},
			bianjiAddress:function(e){
				var data=e.currentTarget.dataset.index
				uni.navigateTo({
					url:"../shopping-goods-address-detail/index?data="+encodeURIComponent(JSON.stringify(data)),
				})
			},
			setMallAddress:function(e){
				var data=e.currentTarget.dataset.index
				if(data.isDefault==1){
					return
				}
				var that=this
				uni.showLoading()
				request.post("api/mall/set_mall_address", {
					name:data.name,
					phone:data.phone,
					address:data.address,
					detail:data.detail,
					isDefault:1,
					id:data.id
				}).then(data => {
					that.getUserAddressList()
				})
			},
			addAddress:function(){
				uni.navigateTo({
					url:"../shopping-goods-address-detail/index",
				})
			},
			selectAddress:function(e){
				var data=e.currentTarget.dataset.index
				uni.$emit('updateAddress', data)
				uni.navigateBack()
			},
		}
	}
</script>

<style>
	@import "index.css";
</style>
